package com.album.activity.page;

import java.lang.ref.SoftReference;
import java.util.HashMap;

import com.album.R;
import com.album.common.Constants; 
import com.album.utils.MyLog;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ImageListAdapter extends CursorAdapter {
	private HashMap<String, SoftReference<Bitmap>> tempMap;
	private Bitmap bitmap;
	private BitmapFactory.Options options; 
    private final Context mContext;
    
	public ImageListAdapter(Context context, Cursor c) {
		super(context, c);
		mContext = context;
		tempMap = new HashMap<String, SoftReference<Bitmap>>(); 
		options = new BitmapFactory.Options();
		options.inSampleSize = 100;
	}
	
	public void setNotifyDataSetChanged(){
		notifyDataSetChanged();
	}
	 
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
//		MyLog.debug(getClass(), "..........................newView...");
		LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		return li.inflate(R.layout.image_item, parent, false);
	}
	
	
	@Override
	public void bindView(View convertView, Context context, Cursor cursor) { 
////		ViewHolder holder;
//		MyLog.info(getClass(), "...bindView...");
		ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
		imageView.setLayoutParams(new RelativeLayout.LayoutParams(Constants.m_screenWidth,Constants.m_screenWidth));
			String value = cursor.getString(cursor.getColumnIndex("_data"));
			try {
				imageView.setImageBitmap(null);
				if (tempMap != null && tempMap.containsKey(value) && tempMap.get(value).get() != null
						&& !tempMap.get(value).get().isRecycled()) { 
					imageView.setImageBitmap(tempMap.get(value).get());
				} else {
					int id = cursor.getInt(cursor.getColumnIndex("_ID"));
					bitmap = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), id,
							MediaStore.Images.Thumbnails.MICRO_KIND, options); 
//					tempMap.put(value, new SoftReference<Bitmap>(bitmap));
					imageView.setImageBitmap(bitmap);
				}
			}catch (Exception e) {
				e.printStackTrace();
			}catch(OutOfMemoryError ex){}
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return super.getCount();
	}
	
	public class ViewHolder {
		
		private ImageView imageView;
		public ImageView getIv() {
			return imageView;
		}
		public void setIv(ImageView iv) {
			this.imageView = iv;
		}
	}
	
}
